package com.mudongheng.app.handler;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.util.SaResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 幕冬
 * @since 2021年10月28日
 */
@ControllerAdvice
public class ControllerExceptionHandler {

    @ExceptionHandler
    @ResponseBody
    public SaResult handleException(Exception e, HttpServletRequest request, HttpServletResponse response) {
        e.printStackTrace();
        if (e instanceof NotLoginException) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        }
        return SaResult.error(e.getMessage());
    }

}
